package cc.admin.modules.graduate.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.modules.graduate.entity.Category;
import cc.admin.modules.graduate.entity.ChoiceQuestions;
import cc.admin.modules.graduate.entity.option;
import cc.admin.modules.sys.entity.SysCascade;
import cc.admin.modules.sys.service.ISysCascadeService;
import cc.admin.modules.graduate.entity.MultiChoiceQuestions;
import cc.admin.modules.graduate.service.IMultiChoiceQuestionsService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 多选题
 * @Author: cc-admin
 * @Date: 2025-02-24
 * @Version: V1.0.0
 */
@Slf4j
@Api(tags = "多选题")
@RestController
@RequestMapping("/multi/choiceQuestions")
public class MultiChoiceQuestionsController extends BaseController<MultiChoiceQuestions, IMultiChoiceQuestionsService> {
    @Autowired
    private IMultiChoiceQuestionsService multiChoiceQuestionsService;

    /**
     * 分页列表查询
     *
     * @param key
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "多选题-分页列表查询")
    @ApiOperation(value = "多选题-分页列表查询", notes = "多选题-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            @RequestParam(name = "key", required = false) String key,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        QueryWrapper<MultiChoiceQuestions> queryWrapper = QueryGenerator.initQueryWrapper(new MultiChoiceQuestions(), req.getParameterMap());
        if (StrUtil.isNotEmpty(key)) {
            queryWrapper.like("question",key);
        }

        Map<String, String[]> parameterMap = req.getParameterMap();
        String[] catalogs = parameterMap.get("catalog");
        String tab = catalogs[0];
        queryWrapper.eq("major_id", tab);
        Page<MultiChoiceQuestions> page = new Page<MultiChoiceQuestions>(pageNo, pageSize);
        IPage<MultiChoiceQuestions> pageList = multiChoiceQuestionsService.page(page, queryWrapper);

        List<MultiChoiceQuestions> records = pageList.getRecords();
        Gson gson = new Gson();
        // 遍历每个选择题
        records.forEach(multiChoiceQuestions -> {
            // 定义每个选择题里面的下拉框
            ArrayList<Category> categories = new ArrayList<>();
            //当存在下拉数据时
            if (multiChoiceQuestions.getCategoryIds() != null && multiChoiceQuestions.getCategoryIdsText() != null) {
                String[] split = multiChoiceQuestions.getCategoryIds().split(",");
                String[] split2 = multiChoiceQuestions.getCategoryIdsText().split(",");
                for (int i = 0; i < split.length; i++) {
                    Category category = new Category(split[i], split2[i]);
                    categories.add(category);
                }
            }
            multiChoiceQuestions.setCategorys(categories);
            java.lang.reflect.Type type = new TypeToken<List<option>>() {}.getType();
            java.lang.reflect.Type stringType = new TypeToken<List<String>>() {}.getType();
            multiChoiceQuestions.setOption(gson.fromJson(multiChoiceQuestions.getOptionJson(), type));
            multiChoiceQuestions.setAnswer(gson.fromJson(multiChoiceQuestions.getAnswerJson(), stringType));
        });
        pageList.setRecords(records);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param multiChoiceQuestions
     * @return
     */
    @AutoLog(value = "多选题-添加")
    @ApiOperation(value = "多选题-添加", notes = "多选题-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody MultiChoiceQuestions multiChoiceQuestions) {
        multiChoiceQuestions.setId(IdUtil.objectId());
        multiChoiceQuestionsService.saveMultiChoice(multiChoiceQuestions);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param multiChoiceQuestions
     * @return
     */
    @AutoLog(value = "多选题-编辑")
    @ApiOperation(value = "多选题-编辑", notes = "多选题-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody MultiChoiceQuestions multiChoiceQuestions) {
        multiChoiceQuestionsService.updateMultiChoice(multiChoiceQuestions);
        return Result.ok("编辑成功!");
    }


    /**
     * 复制
     *
     * @param id
     * @param name
     * @return
     */
    @AutoLog(value = "多选题-复制")
    @ApiOperation(value = "多选题-复制", notes = "多选题-复制")
    @PostMapping(value = "/copy")
    public Result<?> copy(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "name", required = true) String name) {
        MultiChoiceQuestions multiChoiceQuestions = multiChoiceQuestionsService.getById(id);
        multiChoiceQuestions.setId(IdUtil.fastUUID());
        multiChoiceQuestions.setCreateTime(new Date());
        multiChoiceQuestionsService.save(multiChoiceQuestions);
        return Result.ok("复制成功！");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "多选题-通过id删除")
    @ApiOperation(value = "多选题-通过id删除", notes = "多选题-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        multiChoiceQuestionsService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "多选题-批量删除")
    @ApiOperation(value = "多选题-批量删除", notes = "多选题-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.multiChoiceQuestionsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "多选题-通过id查询")
    @ApiOperation(value = "多选题-通过id查询", notes = "多选题-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        MultiChoiceQuestions multiChoiceQuestions = multiChoiceQuestionsService.getById(id);
        return Result.ok(multiChoiceQuestions);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param
     */
    @GetMapping(value = "/exportXls")
    public ModelAndView exportXls(@RequestParam(name="choiceQuestions",required = false) String choiceQuestionIds,@RequestParam(name="key",required = false) String key,@RequestParam(name="catalog",required = false) String catalog,HttpServletRequest request) {
        return multiChoiceQuestionsService.exportXls(request, choiceQuestionIds,catalog, MultiChoiceQuestions.class, "选择题");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return multiChoiceQuestionsService.importExcel(request, response, MultiChoiceQuestions.class);
    }

}
